<?php
include ("configs/config.php");
if ( $_SESSION['LOGIN_NAME'] <> "admin" ) 
	{
	header("Location: login-admin.php");
	exit();
	}

$tpl = new XTemplate("./template/tpl_admin.html");
$tpl->parse("main.body.menu");

if (isset($_POST['gallery'])){
	$_SESSION['gallery'] = $_POST['gallery'];
}
if (!isset($_SESSION['gallery'])) $_SESSION['gallery'] = 'ALLPICS';
$query=mysql_query("SELECT * FROM a_gallery order by name");
$group = '<option value="ALLPICS">all</option>';
$galleries = array();
if (mysql_num_rows($query)>0)
 {
 	while ($temp = mysql_fetch_array($query))
 		{
 		 $selected = '';
 		 if ($_SESSION['gallery']==$temp['id']) $selected=' selected="selected" ';	
 		 $galleries[$temp['id']] = $temp['name']; 
 		 $group.='<option '.$selected.' value="'.$temp['id'].'">'.$temp['name'].'</option>';
 		}

 }
 else
 {
 	$msg_error="There was a error. <BR> Please verify :<BR>";
 	$error++;
 	$msg_error.="There was a problem with the database! Please come back in 10 minutes";	
 	exit();
 }
$error=0;
$tpl->assign("gallery",$group);
$error=0;
$tpl->assign("edit_picture","Manage Pictures (in galleries)");
if ($_GET["clicked"]==1)
{
	
		//$title=$_GET["title"];
		$description=$_GET["description"];
        $url = addHttpString($_GET["picture_url"]);
		$picture_title=$_GET["picture_title"];
		$id=$_GET["id"];
		$title=$_GET["title"];
		$add_date=$_GET['add_date'];
		$name=$_GET['name'];
		$gallery_id = $_GET['gallery_id'];
/*		$old_title=$_GET["old_title"];
		
		if ($_GET["type"]=="Banner")
		{	
			
	
			if ($picture_title<>"" and $title <> "" and $url <> "")
			{
			
				$query=('update pictures set url="'.$url.'",picture_title="'.$picture_title.'" ,name="'.$title.'" where id='.$id.';');
				mysql_query($query);
				
			}
		}	
		
		if ($_GET["type"]=="Picture")
		{
			if ($title<>"")
			{*/
				$sql=('update a_gallerypics set add_date="'.$add_date.'", name="'.$name.'", gallery_id="'.$gallery_id.'", description="'.$description.'",picture_title="'.$picture_title.'",picture_url="'.$url.'" where id='.$id.';');				
				mysql_query($sql) or die(mysql_error().$sql);
				
/*				
			}
		}
		
		if ($_GET["type"]=="Link")
		{
			$query='update pictures set picture_title="'.$picture_title.'",url="'.$url.'" where id='.$id.';';
			mysql_query($query);
	
			
		}
*/		
		if (mysql_affected_rows()<1)
		{
			$error++;
			//$msg_error="There was a problem with the database! Please come back in 10 minutes";
		}
		else
		{
//			echo $PATH."pictures/$old_title.jpg </br>";
//			echo $PATH."pictures/$title.jpg  </br>";
			rename($AG_PATH."pictures/$old_title.jpg",$AG_PATH."pictures/$name.jpg");
			rename($AG_PATH."pictures/thumb-$old_title.jpg",$AG_PATH."pictures/thumb-$name.jpg");	
			
			header("Location: edit-picture-gallery.php");
			exit();
		}
	
	
}


/*if ($_GET["archive"]<>"")
{	
	$id=$_GET["archive"];
	 $id++;
	$query="update pictures set id=id+1 order by id desc";
	
	mysql_query($query);
	$query ="update pictures_group set pictures=pictures+1 order by pictures desc";
	
	mysql_query($query);
	$rez=mysql_query("select min(id) from pictures");
	$rez1=mysql_fetch_array($rez);
	
	$rez1[0]--;
	$query="update pictures set id=$rez1[0],first_page=0 where id=$id";
	
	mysql_query($query);
	$query="update pictures_group set pictures=$rez1[0] where pictures=$id";
	mysql_query($query);
	
	
	
}




if($_GET["first_page"]<>"")
{
	$id=$_GET["id"];

	if ($_GET["first_page"]=="yes")
	{
	
		$query=("update pictures set first_page=0 where id=$id");
	}
	else
	{
		$query=("update pictures set first_page=1 where id=$id and validation=1");
	}
	
	mysql_query($query)	;

	if (mysql_affected_rows()<1)
	{
		$error++;
		$msg_error="The picture is not validated or there was a problem with the database";
		
	}
	else
	{
		header("Location: edit-picture.php");
		exit();
	}

}

if (isset($_GET["validate"]))
{
	if ($_GET["validate"]=="invalidate")
		{$query=("update pictures set validation=".$_GET["validate"].", first_page=0 where id=".$_GET["id"].";");}
	else
		{$query=("update pictures set validation=".$_GET["validate"].", first_page=0 where id=".$_GET["id"].";");}

	mysql_query($query);
	if (mysql_affected_rows()<1)
	{
		$error++;
		$msg_error="There was a problem with the database! Please come back in 10 minutes";
		
	}
	else
	{
		header("Location: edit-picture.php");
		exit();
	}

	
}
*/
if ($_GET["delete"]<>"" )
{
	$id=$_GET["delete"];
	$query=("delete from a_gallerypics where id=".$id.";");
	mysql_query($query);

	if (mysql_affected_rows()==-1)
	{
		$error++;
		$msg_error="There was a problem with the database! Please come back in 10 minutes";
			
	}
	else
	{
		$title=$_GET["title"];
		unlink($AG_PATH."pictures/$title.jpg");
		unlink($AG_PATH."pictures/thumb-$title.jpg");
		
		header("Location: edit-picture-gallery.php");
		exit();
	}
}



if ($_GET["position"]<>"")
{
	if ($_GET["position"]=="up")
	{
		$id=$_GET["id"];
		move_record($id,'desc','pictures','id');
	}
	else
	{
		$id=$_GET["id"];
	 	move_record($id,"asc","pictures","id");
	}	 
	 
	header("Location: edit-picture.php");
	exit();	
}

if ($_SESSION["entries"]=="")
{
		$_SESSION["entries"]=30;
}
else	
{
	if ($_POST["entries"]<>"")
	{
		$_SESSION["entries"]=$_POST["entries"];
	}
	
}	




$entries=30;
if(isset($_GET['pv_page'])) $cur_page=$_GET['pv_page'];
else $cur_page = 0;
$pstart = $cur_page*$entries;
$pics_amount = db_getTotalPicsAmount(($_SESSION['gallery']=='ALLPICS')?NULL:$_SESSION['gallery']);
$tpl->assign("entries",$entries);
if ($_SESSION['gallery']=='ALLPICS') $query=("select * from a_gallerypics order by id  desc limit $pstart,$entries;");
else $query=("select * from a_gallerypics WHERE gallery_id={$_SESSION['gallery']} order by id  desc limit $pstart,$entries;");
$result=mysql_query($query);
if (mysql_num_rows($result)>0)
{
	while($picture=mysql_fetch_assoc($result))
	{
		$galopts = '';
		foreach($galleries as $k=>$v){
			$galopts.= "<option value=\"$k\"";
			if ($picture['gallery_id']==$k) $galopts.=' selected="selected" ';
			$galopts.=">$v</option>";
		}
		$tpl->assign('gallery_opts',$galopts);
		$tpl->assign('thumb','../gallery_pics/pictures/thumb-'.$picture['name'].'.jpg');
		$tpl->assign($picture);
	$tpl->parse("main.body.edit_picture_gallery.row");	
	}
}


$tpl->assign("msg_error",$msg_error);
$tpl->parse("main.body.edit_picture_gallery.error");
parsePaging($cur_page, $pics_amount, $entries, 'edit-picture-gallery.php?', 'main.body.edit_picture_gallery.');
$tpl->parse("main.body.edit_picture_gallery");
$tpl->parse("main.body");
$tpl->parse("main");
$tpl->out("main");
//
/**
 * Shows pagination
 * @param int $cur_page
 * @param int $total
 * @param int $perpage 
 * @param string $link 
 * @param string $tplpath path within template, must have '.' at the end 
 */
function parsePaging($cur_page, $total, $perpage, $link, $tplpath){
	//don't show pager links if there isn't any records
	if ($total==0) return;
	//don't show pager links if the all records are fit into single page
	if ($total<=$perpage) return;
	global $tpl;
	$link.= '&pv_page=';
	//total
	$tpl->assign('paging_cur',$cur_page+1);
	$pages = ceil($total/$perpage);
	$tpl->assign('paging_total_pages',$pages);
	//first 
	if ($cur_page==0){
		$tpl->parse($tplpath.'paging.first_disabled');
	}else{
		$tpl->assign('paging_first',$link.'0');
		$tpl->assign('paging_prev',$link.($cur_page-1));
		$tpl->parse($tplpath.'paging.first');
		$tpl->parse($tplpath.'paging.prev');
	}
	//last
	if(($total-$cur_page*$perpage)<=$perpage){
		$tpl->parse($tplpath.'paging.last_disabled');
	}else{
		$tpl->assign('paging_last',$link.($pages-1));
		$tpl->assign('paging_next',$link.($cur_page+1));
		$tpl->parse($tplpath.'paging.next');
		$tpl->parse($tplpath.'paging.last');		
	}
	$tpl->parse($tplpath.'paging');
}
/**
 * counts total amount of pictures in the given gallery
 * If $gallery_id==NULL returns total amount in all galleries 
 * @param int $gallery_id
 * @return int
 */
function db_getTotalPicsAmount($gallery_id=NULL){
	$sql = "SELECT COUNT(id) AS amount FROM a_gallerypics ";
	if ($gallery_id!==NULL) $sql.= "WHERE gallery_id='$gallery_id' ";
	$query = mysql_query($sql) or die(mysql_error().$sql);
	if ($query){
		 $row = mysql_fetch_assoc($query);
		 if (isset($row['amount'])) return $row['amount'];
	}
	return 0;
}
?>